//package com.zy.config.common;
//
//import com.zy.config.feign.PropertyFeign;
//import com.zy.config.utils.RemoteConfigProviderFactory;
//import com.zy.config.utils.RemoteConfigSupport;
//import lombok.SneakyThrows;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.BeansException;
//import org.springframework.beans.SimpleTypeConverter;
//import org.springframework.beans.factory.BeanFactory;
//import org.springframework.beans.factory.BeanFactoryAware;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.beans.factory.config.ConfigurableBeanFactory;
//import org.springframework.context.EnvironmentAware;
//import org.springframework.core.env.ConfigurableEnvironment;
//import org.springframework.core.env.Environment;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.Resource;
//import javax.servlet.ServletContextEvent;
//import javax.servlet.ServletContextListener;
//import java.io.StringReader;
//import java.sql.Connection;
//import java.sql.DriverManager;
//import java.sql.SQLException;
//import java.util.Optional;
//import java.util.Properties;
//
//@Slf4j
//@Component
//public class ServletContextListenerImpl extends RemoteConfigSupport implements ServletContextListener, BeanFactoryAware, EnvironmentAware {
//
//    private ConfigurableEnvironment environment;
//
//    private ConfigurableBeanFactory beanFactory;
//
//    private SimpleTypeConverter typeConverter;
//
//    private RemoteConfigProviderFactory providerFactory;
//
//    @Resource
//    private PropertyFeign propertyFeign;
//
//    @Value("${spring.application.name}")
//    private String appId;
//
//    private final String CONFIG = "config";
//
//
//    /**
//     * 静态代码块会在依赖注入后自动执行,并优先执行
//     */
//    static{
//        log.info("启动时自动执行 静态代码块");
//    }
//    /**
//     * 在初始化Web应用程序中的任何过滤器或Servlet之前，将通知所有ServletContextListener上下文初始化。
//     */
//    @SneakyThrows
//    @Override
//    public void contextInitialized(ServletContextEvent sce) {
//
//        if (!appId.equals(CONFIG)) {
//            String str = propertyFeign.propertyToString(appId);
//            Properties current = new Properties();
//            current.load(new StringReader(str));
//            if (Optional.ofNullable(current).isPresent()) {
//                log.info("开始更新Environment------------------------appId:【{}】",appId);
//                // 更新Environment
//                addPropertySource(this.environment, current);
//                // 更新bean实例的成员变量属性值
//                updateField(beanFactory, environment, typeConverter, current);
//            }
//        }
//    }
//
//    @Override
//    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
//        this.beanFactory = (ConfigurableBeanFactory) beanFactory;
//        this.typeConverter = new SimpleTypeConverter();
//        this.typeConverter.setConversionService(this.beanFactory.getConversionService());
//    }
//
//    @Override
//    public void setEnvironment(Environment environment) {
//        this.environment = (ConfigurableEnvironment) environment;
//        this.providerFactory = new RemoteConfigProviderFactory(this.environment);
//    }
//
//    private static boolean testConnection(String code, String username, String password, String url, String driver) {
//        log.info("test connection start. code:{}", code);
//        Connection var2 = null;
//        try {
//            Class.forName(driver);
//        } catch (ClassNotFoundException e) {
//            log.warn("class not found. code: {}", code, e);
//            return false;
//        }
//        try {
////            JdbcSecurityUtil.validate(url);
//            DriverManager.setLoginTimeout(3);
//            var2 = DriverManager.getConnection(url, username, password);
//            if (var2 == null) {
//                log.warn("connection fail. code: {}", code);
//                return false;
//            }
//            log.info("test connection success. code:{}", code);
//        } catch (Exception e) {
//            log.warn("connection error. code: {}", code, e);
//            return false;
//        } finally {
//            try {
//                if (var2 != null && !var2.isClosed()) {
//                    var2.close();
//                }
//            } catch (SQLException e) {
//                log.warn(e.toString());
//            }
//        }
//        return true;
//    }
//}
